package com.ukao.cashregister.ui.login;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.ukao.cashregister.R;
import com.ukao.cashregister.base.MvpFragment;
import com.ukao.cashregister.bean.AccountLoginBean;
import com.ukao.cashregister.bean.TokenBean;
import com.ukao.cashregister.consts.Constant;
import com.ukao.cashregister.consts.SaveParamets;
import com.ukao.cashregister.listener.MyTextWatcher;
import com.ukao.cashregister.pesenter.AccountLoginPesenter;
import com.ukao.cashregister.utils.CheckUtils;
import com.ukao.cashregister.utils.T;
import com.ukao.cashregister.utils.Utils;
import com.ukao.cashregister.view.AccountLoginView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

import static com.ukao.cashregister.R.id.head_image;
import static com.ukao.cashregister.consts.SaveParamets.setAccount;

/**
 * 账号登陆
 */
public class AccountLoginFragment extends MvpFragment<AccountLoginPesenter> implements AccountLoginView {

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    Unbinder unbinder;
    @BindView(R.id.pass)
    EditText pass;
    @BindView(R.id.is_login_image)
    Button isLoginImage;
    @BindView(head_image)
    ImageView headImage;
    @BindView(R.id.use_name)
    EditText useName;



    private String mParam1;
    private String mParam2;


    private String account;
    private String password;


    public static AccountLoginFragment newInstance(String param1, String param2) {
        AccountLoginFragment fragment = new AccountLoginFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    protected AccountLoginPesenter createPresenter() {
        return new AccountLoginPesenter(this);
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_account_login, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        initView();
        initListener();
    }

    @Override
    protected void initView() {

        useName.setText(SaveParamets.getAccount());




       // addLayoutListener();
    }

    @Override
    protected void initListener() {
        super.initListener();
        useName.addTextChangedListener(useNameTextWatcher);
        pass.addTextChangedListener(passTextWatcher);
    }

    private MyTextWatcher useNameTextWatcher = new MyTextWatcher() {
        @Override
        public void afterTextChanged(Editable s) {
            super.afterTextChanged(s);
            isILoginGo(s, pass);

        }
    };


    private MyTextWatcher passTextWatcher = new MyTextWatcher() {
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            super.onTextChanged(s, start, before, count);
            isILoginGo(s, useName);
        }

        @Override
        public void afterTextChanged(Editable s) {
            super.afterTextChanged(s);


        }


    };

//    public void addLayoutListener() {
//        mainLl.setKeyboardListener(new KeyboardLayout.KeyboardLayoutListener() {
//            @Override
//            public void onKeyboardStateChanged(boolean isActive, int keyboardHeight) {
//                Log.e("onKeyboardStateChanged", "isActive:" + isActive + " keyboardHeight:" + keyboardHeight);
//                if (isActive) {
//                    scrollToBottom();
//                }
//            }
//        });
//    }
//
//    /**
//     * 弹出软键盘时将SVContainer滑到底
//     */
//    private void scrollToBottom() {
//
//        loginLl.postDelayed(new Runnable() {
//
//            @Override
//            public void run() {
//                loginLl.smoothScrollTo(0, loginLl.getBottom() + SoftKeyInputHidWidget.getStatusBarHeight(_mActivity));
//            }
//        }, 100);
//
//    }

    private void isILoginGo(CharSequence s, EditText mEditText) {
        String strPass = s.toString().trim();
        String strUseName = getText(mEditText);
        if (strPass.isEmpty() || strUseName.isEmpty()) {
            isLoginImage.setAlpha(0.5f);
            isLoginImage.setEnabled(false);
        } else {
            isLoginImage.setAlpha(1f);
            isLoginImage.setEnabled(true);
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }


    @OnClick({head_image, R.id.is_login_image})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case head_image:


                break;
            case R.id.is_login_image:

                account = getText(useName);
                password = getText(pass);
                if (CheckUtils.isEmpty(account)) {
                    T.show("请输入账号");
                    return;
                } else if (CheckUtils.isEmpty(password)) {
                    T.show("请输入密码");
                    return;
                }

                mvpPresenter.appSecretData();


                break;
        }
    }


    @Override
    public void loadloginSucceed(AccountLoginBean mitem) {
        setAccount(account);
        startActivity(Constant.loginSucceedJump(_mActivity, mitem));
        getActivity().finish();
        //AppManager.getAppManager().finishActivity(LoginActivity.class);
    }

    @Override
    public void loadFailure(String message) {
        T.show(message);
    }

    @Override
    public void appSecretSucceed(TokenBean mTokenBean) {
        SaveParamets.saveloginSign(Utils.getContext(), mTokenBean.getData().getAppSecret());
        mvpPresenter.loginRequest(_mActivity, account, password);

    }

}
